import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {ApiRes} from '../common/model/response';
import {map} from 'rxjs/operators';
import {HttpClient} from '@angular/common/http';
import {UrlService} from '../common/service/url.service';
import {SearchResult} from '../content/model/search-result';
import {ScienceCondition} from './magic-science-innovate.component';
import {RegionPolicyCondition} from '../content/model/region-policy-condition';
import {AreaAnalysisCondition} from "../content/model/area-analysis-condition";

@Injectable({
  providedIn: 'root'
})
export class MagicScienceInnovateService {

  constructor(private http: HttpClient,
              private urlService: UrlService) {
  }

  getTreeData(): Observable<ApiRes> {
    const url = this.urlService.getWebUrl('/policySearch/wordFreamList');
    return this.http.get<ApiRes>(url);

  }

  searchSciencePolicy(condition: ScienceCondition): Observable<ApiRes> {
    const url = this.urlService.getWebUrl('/policyAnalysis/kjcx');
    // @ts-ignore
    return this.http.get<ApiRes>(url, {params: condition});

  }

  getPolicySortRegion(condition: AreaAnalysisCondition): Observable<ApiRes> {
    const url = this.urlService.getWebUrl('/policySearch/searchV2');
    return this.http.post<ApiRes>(url, condition).pipe(map(res => {
      const resultTemp = res.result;

      for (const i of Object.keys(resultTemp)) {
        switch (i) {
          case '全国':
            resultTemp[i].imgSrc = '../../../assets/image/magic-science-innovate/country.png';
            break;
          case '上海':
            resultTemp[i].imgSrc = '../../../assets/image/magic-science-innovate/shanghai.png';
            break;
          case '北京':
            resultTemp[i].imgSrc = '../../../assets/image/magic-science-innovate/beijing.png';
            break;
          case '广东':
            resultTemp[i].imgSrc = '../../../assets/image/magic-science-innovate/guangdong.png';
            break;
          case '浙江':
            resultTemp[i].imgSrc = '../../../assets/image/magic-science-innovate/zhejiang.png';
            break;
          case '江苏':
            resultTemp[i].imgSrc = '../../../assets/image/magic-science-innovate/jiangsu.png';
            break;
          case '其他城市':
            resultTemp[i].imgSrc = '../../../assets/image/magic-science-innovate/other-cities.png';
            break;
        }

      }
      res.result = resultTemp;
      return res;
    }));

  }
}
